﻿/*****************************************************************************
* File			: src.Background
* Purpose		: Contains Declaration and Implementation for Background class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip
	import flash.events.*
	import flash.utils.Timer
	

	/**************************************
	* Class		: Background
	* Purpose	: Logic for background behavior
	* Parent	: MovieClip
	**************************************/
	public class Background extends MovieClip
	{
		// Class Variables
		public static var nSpeed;		// Scrolling speed
		
		// Constructor
		public function Background()
		{
			nSpeed = 0;	// Init speed to 0
			
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			cacheAsBitmap = true;
		}
		
		/****************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: e:Event received when entering frame
		****************************************/
		public function enterFrame(e:Event)
		{
			// Scroll background 
			x -= nSpeed;
			
			// Reset position
			if (x < -Game.WIDTH)
				x=0;
		}
		
		/***********************************************************
		* Method		: stopScrolling
		* Purpose		: manages progressive scrolling stop
		* Parameters	: None
		* return		: void
		***********************************************************/
		public static function stopScrolling():void 
		{
			if (nSpeed < 0)
				return;
				
			// Use timers to progressively decrease background speed to stop it
			var decelerationTimer:Timer = new Timer(2000/60,20);
			decelerationTimer.addEventListener(TimerEvent.TIMER, Background.slowingHandler);
			decelerationTimer.start();  
        }
		
		/***********************************************************
		* Method		: startScrolling
		* Purpose		: manages progressive scrolling start
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public static function startScrolling():void 
		{
			if (nSpeed > 0)
				return;
				
			// Use timers to progressively decrease background speed to stop it
			var decelerationTimer:Timer = new Timer(2000/60,20);
			decelerationTimer.addEventListener(TimerEvent.TIMER, Background.accelerationHandler);
			decelerationTimer.start();  
        }
		
		/***********************************************************
		* Method		: accelerationHandler
		* Purpose		: manages background acceleration
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public static function accelerationHandler(event:TimerEvent):void 
		{
			//  Increse bg speeds
			nSpeed += 6/Game.FPS;   
        }
		
		/***********************************************************
		* Method		: slowingHandler
		* Purpose		: manages background deceleration
		* Parameters	: e:Event
		* return		: void
		***********************************************************/
		public static function slowingHandler(event:TimerEvent):void 
		{
			//  Increse bg speeds
			nSpeed -= 6/Game.FPS;   
        }
	}
}